package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.sxm.yiwei.R;

import java.util.ArrayList;
import java.util.List;

public class HopeCustomActivity extends BaseActivity implements View.OnClickListener{

    private ImageView customback;
    private EditText hopeingrade;
    private EditText hopeindank;
    private Button hopeyes;
    private Button hopeno;
    private Button hopeback;
    private Button hopecancel;
    private Button sexno;
    private Button sexman;
    private Button sexwoman;
    private Spinner danksp;
    private Spinner gradesp;
    private List<String> danklist;
    private List<String> gradelist;
    private int index;
    private Editable text;
    private String dank;
    private int isstatus;
    private int issex;
    private int index1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hope_custom);
        inithopeCustomView();
    }

    private void inithopeCustomView() {
        customback = (ImageView) findViewById(R.id.hope_custom_back);
        hopeingrade = (EditText) findViewById(R.id.hope_inputgrade);
        hopeindank = (EditText) findViewById(R.id.hope_inputdank);
        hopeyes = (Button) findViewById(R.id.hope_yesbt);
        hopeno = (Button) findViewById(R.id.hope_nobt);
        hopeback = (Button) findViewById(R.id.hope_custombackbt);
        hopecancel = (Button) findViewById(R.id.hope_customcancel);
        sexno = (Button) findViewById(R.id.hope_sexno);
        sexman = (Button) findViewById(R.id.hope_sexman);
        sexwoman = (Button) findViewById(R.id.hope_sexwoman);
        danksp = (Spinner) findViewById(R.id.custom_danksp);
        gradesp = (Spinner) findViewById(R.id.custom_gradesp);
        hopeingrade.setText("普通");
        hopeindank.setText("1");
        danklist = new ArrayList<>();
        danklist.add("选");
        for (int i = 1; i < 21; i++) {
            danklist.add(String.valueOf(i)+"   ");
        }
        final ArrayAdapter dadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,danklist);
        dadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        danksp.setAdapter(dadapter);
        danksp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //选中item
                String item = String.valueOf(dadapter.getItem(position));
                hopeindank.setText(item);
                index1 = position;
                parent.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 没有选择
                parent.setVisibility(View.VISIBLE);
            }
        });
        gradelist = new ArrayList<>();
        gradelist.add("选");
        gradelist.add("小工");
        gradelist.add("贫农");
        gradelist.add("富农");
        gradelist.add("地主");
        gradelist.add("知县");
        gradelist.add("知府");
        gradelist.add("总督");
        gradelist.add("王爷");
        gradelist.add("帝王");


        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,gradelist);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        gradesp.setAdapter(adapter);
        gradesp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                hopeingrade.setText(String.valueOf(adapter.getItem(position)));
                index = position;
                parent.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                parent.setVisibility(View.VISIBLE);
            }
        });

        // 点击监听
        customback.setOnClickListener(this);
        hopeno.setOnClickListener(this);
        hopeyes.setOnClickListener(this);
        hopeback.setOnClickListener(this);
        hopecancel.setOnClickListener(this);
        sexman.setOnClickListener(this);
        sexno.setOnClickListener(this);
        sexwoman.setOnClickListener(this);
        // 设置默认
        hopeyes.setSelected(true);
        sexno.setSelected(true);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.hope_custom_back:
                finish();
                break;
            case R.id.hope_yesbt:
                isstatus=1;
                hopeyes.setSelected(true);
                hopeno.setSelected(false);
                break;
            case R.id.hope_nobt:
                isstatus=0;
                hopeno.setSelected(true);
                hopeyes.setSelected(false);
                break;
            case R.id.hope_sexno:
                sexno.setSelected(true);
                sexwoman.setSelected(false);
                sexman.setSelected(false);
                issex=0;
                break;
            case R.id.hope_sexman:
                issex=2;
                sexno.setSelected(false);
                sexwoman.setSelected(false);
                sexman.setSelected(true);
                break;
            case R.id.hope_sexwoman:
                issex=1;
                sexno.setSelected(false);
                sexwoman.setSelected(true);
                sexman.setSelected(false);
                break;
            case R.id.hope_custombackbt:
                finish();
                break;
            case R.id.hope_customcancel:
                Intent data = getIntent();
                Bundle bundle = new Bundle();
                bundle.putString("dank",String.valueOf(index1));
                bundle.putString("issex",String.valueOf(issex));
                bundle.putString("isstatus",String.valueOf(isstatus));
                bundle.putString("grade",String.valueOf(index));
                data.putExtras(bundle);
                HopeCustomActivity.this.setResult(RESULT_OK,data);
                HopeCustomActivity.this.finish();
                break;

        }
    }


}
